////////////////////////////////////////////////////////////
// Pocket PC Game Programming
// Chapter 10: Sound Effects and Music
//
// CWaveOut Source File
//
// This file includes the CWaveOut definition.
// Only supports uncompressed Windows PCM wave format.
// 
////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CWaveOut.h"

////////////////////////////////////////////////////////////
// CWaveOut Constructor
//
// This function is called when the class is instantiated.
//
CWaveOut::CWaveOut()
{
    dwBytesRead = 0;
    lpWave = NULL;
    bRepeat = FALSE;
}

////////////////////////////////////////////////////////////
// CWaveOut Constructor (Overloaded)
//
// This function is called when the class is instantiated.
// Loads the wave passed as a parameter.
//
CWaveOut::CWaveOut(LPTSTR lpFile)
{
    dwBytesRead = 0;
    lpWave = NULL;
    if (wcslen(lpFile) > 0)
        Load(lpFile);
}

////////////////////////////////////////////////////////////
// CWaveOut Destructor
//
// This function is called when the class is terminated.
//
CWaveOut::~CWaveOut()
{
    if (lpWave != NULL)
    {
        Stop();
        free((void *)lpWave);
    }
}

////////////////////////////////////////////////////////////
// CWaveOut::Play
//
// Plays the wave buffer from memory (if loaded).
//
void CWaveOut::Play()
{
    if (lpWave != NULL)
    {
        if (bRepeat)
            sndPlaySound(lpWave, SND_MEMORY | SND_ASYNC | SND_LOOP);
        else
            sndPlaySound(lpWave, SND_MEMORY | SND_ASYNC);

    }
}

////////////////////////////////////////////////////////////
// CWaveOut::Stop
//
// Stops playback of the wave buffer.
//
void CWaveOut::Stop()
{
    sndPlaySound(NULL, 0);
}

////////////////////////////////////////////////////////////
// CWaveOut::Load
//
// Loads an uncompressed PCM wave file from disk.
//
BOOL CWaveOut::Load(LPTSTR lpFile)
{
	HANDLE hFile;
    DWORD dwSize;
    BOOL bRet;

    //first make sure a wave is not already loaded
    if (lpWave != NULL)
    {
        Stop();
        free((void *)lpWave);
    }

    //open the file
    hFile = CreateFile(lpFile, GENERIC_READ, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }

    //determine wave size in bytes
    dwSize = GetFileSize(hFile, NULL);
    if (dwSize == 0xFFFFFFFF)
    {
        CloseHandle(hFile);
        return FALSE;
    }
 
	//allocate memory for the wave
	lpWave = (LPCTSTR)malloc(dwSize);
    if (lpWave == NULL)
	{
		CloseHandle(hFile);
		return FALSE;
	}

	//read the wave file
	bRet = ReadFile(hFile, (void *)lpWave, dwSize, &dwBytesRead, NULL);
    if ((bRet == FALSE) || (dwBytesRead != dwSize))
	{
        CloseHandle(hFile);
        free((void *)lpWave);
        dwBytesRead = 0;
		return FALSE;
	}
    
    CloseHandle(hFile);
    return TRUE;
}

